/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import nl.cloudfarming.client.model.DataProvider;

/**
 *
 * A layer represents a set of data to be shown on the
 * map in a layer.
 *
 *
 * @author Timon Veenstra
 */
public abstract class ObjectLayer<O extends LayerObject> extends BaseLayer {

    private final List<O> objects = new ArrayList<O>();
    private final List<O> selectedObjects = new ArrayList<O>();
    
    public static final String PROPERTY_OBJECTS = "objects";
    private boolean interActive = false;

    public ObjectLayer(Palette palette, Category category, String name, DataProvider dataProvider) {
        super(palette, category, name,dataProvider);
    }

    public ObjectLayer(Palette palette, Category category, String name, DataProvider dataProvider, boolean interActive) {
        super(palette, category, name,dataProvider);
        this.interActive = interActive;
    }

    /**
     * get a list of objects in the layer
     * Objects extend the LayerObject interface which contains
     * a geometry.
     *
     * @return
     */
    public List<O> getObjects() {
        return Collections.unmodifiableList(this.objects);
    }

    /**
     * Add a layerObject to the internal objects list
     *
     * @param object
     */
    public void addObject(O object) {
        List<O> objectsCopy = new ArrayList<O>(this.objects);
        this.objects.add(object);
        getChangeSupport().firePropertyChange(PROPERTY_OBJECTS, objectsCopy, this.objects);
    }

    /**
     * remove a layerObject from the internal objects list
     *
     * @param object
     */
    public void removeObject(O object) {
        if (this.objects.contains(object)) {
            List<O> objectsCopy = new ArrayList<O>(this.objects);
            this.objects.remove(object);
            getChangeSupport().firePropertyChange(PROPERTY_OBJECTS, objectsCopy, this.objects);

        }
    }

    /**
     * clear the objects list
     */
    public void clearObjects() {
        List<O> objectsCopy = new ArrayList<O>(this.objects);
        this.objects.clear();
        getChangeSupport().firePropertyChange(PROPERTY_OBJECTS, objectsCopy, this.objects);
    }

    /**
     * 
     * @param objectsToAdd
     */
    public void addObjects(List<O> objectsToAdd) {
        List<O> objectsCopy = new ArrayList<O>(this.objects);
        this.objects.addAll(objectsToAdd);
        getChangeSupport().firePropertyChange(PROPERTY_OBJECTS, objectsCopy, this.objects);
    }

    /**
     * if a use can interact with the layer or not
     * 
     * @return
     */
    public boolean isInterActive() {
        return interActive;
    }

    /**
     *
     *
     * @param interActive
     */
    public void setInterActive(boolean interActive) {
        getChangeSupport().firePropertyChange(PROPERTY_INTER_ACTIVE, this.interActive, interActive);
        this.interActive = interActive;
    }

    public List<O> getSelectedObjects() {
        return selectedObjects;
    }

}
